﻿using System;
using System.Collections.Generic;
using System.Reflection;

namespace Gaea.CodeFirst
{
    public class AlterTableBasic
    {
        public void BasicAlterTable(OperateTableContext operateTableContext)
        {
            PropertyInfo[] propertyInfo = operateTableContext.typeOfGaeaSon.GetProperties();
            string ColumnSql = "";
            string sql = "";
            string ColumnName = "";
            string ColumnType = "";
            Dictionary<string, string> keyValuePairs = new Dictionary<string, string>();

            if (operateTableContext.DbName == EnumDbName.MySql)
            {
                var enumerator = operateTableContext.allMySqlColumns.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    ColumnName = enumerator.Current.Field;
                    ColumnType = enumerator.Current.Type;
                    keyValuePairs.Add(ColumnName, ColumnType);    //将数据库表所有字段名及类型存入字典
                }

            }
            if (operateTableContext.DbName == EnumDbName.SqlServer)
            {
                var enumerator = operateTableContext.allSqlServerColumns.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    ColumnName = enumerator.Current.COLUMN_NAME;
                    ColumnType = enumerator.Current.DATA_TYPE;
                    if (ColumnType == "varchar")
                    {
                        ColumnType = enumerator.Current.DATA_TYPE + "(" + enumerator.Current.CHARACTER_MAXIMUM_LENGTH + ")";
                    }
                    keyValuePairs.Add(ColumnName, ColumnType);    //将数据库表所有字段名及类型存入字典

                }
            }

            foreach (var property in propertyInfo)
            {
                var column_name = property.Name;
                var column_type = property.PropertyType.Name;

                var attributes = property.GetCustomAttributes();
                ConvertType convertType = new ConvertType();
                column_type = convertType.ConvertMySqlType(attributes, column_type);

                //与字典内数据对比，将没有的字段在数据表内新增
                if (keyValuePairs.ContainsKey(column_name) == false)
                {
                    if (operateTableContext.DbName == EnumDbName.MySql)
                    {
                        ColumnSql = column_name + " " + column_type;
                        sql = "Alter TABLE " + operateTableContext.TableName + " Add " + ColumnSql + " ;";
                        if (column_name == "Id")
                        {
                            sql = "alter table " + operateTableContext.TableName + " modify Id int not null primary key auto_increment;";
                        }
                        operateTableContext.gaeaBasicPower.ExecuteAsync(sql, null);
                    }
                    else if (operateTableContext.DbName == EnumDbName.SqlServer)
                    {
                        if (column_name == "Id")
                        {
                            sql = "alter table " + operateTableContext.TableName + " add Id int not null primary key Identity(1,1);";
                            operateTableContext.gaeaBasicPower.ExecuteAsync(sql, null);
                        }
                        else
                        {
                            ColumnSql = column_name + " " + column_type + " not null";
                            sql = "Alter TABLE " + operateTableContext.TableName + " Add " + ColumnSql + " ;";
                            operateTableContext.gaeaBasicPower.ExecuteAsync(sql, null);
                        }
                    }
                }
                //包含该字段名，但类型不同的报错
                else if (keyValuePairs.ContainsKey(column_name) == true && keyValuePairs[column_name] != column_type.ToLower() && column_name != "Id")
                {
                    Console.WriteLine(column_name + " is already exists,Db Type is " + keyValuePairs[column_name] + " ,and code Type is " + column_type);
                }
            }
        }
    }
}
